﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using BLL;
using BE;
using System.Data;

namespace UI
{
    public partial class EditForm : System.Web.UI.Page
    { 
        DataTable taskTable;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                makeGrid();
            }
        }

        private void makeGrid()
        {
			string item = Request.QueryString["action"];

            if (item == "Room")
                gridRooms();
            else if (item == "Location")
                gridVertices();
            else if (item == "Walkway")
                gridEdges();
        }

        private void gridEdges()
        {
            lblEditTitle.Text = "Edit Walkway";

            EdgeWorker ew = new EdgeWorker();
            List<Edge> edges = ew.GetAll();

            taskTable = new DataTable("TaskList");

            taskTable.Columns.Add("Id", typeof(int));
            taskTable.Columns.Add("Name", typeof(string));
            taskTable.Columns.Add("Weight", typeof(int));
            taskTable.Columns.Add("Straight", typeof(bool));
            taskTable.Columns.Add("1. Location ID", typeof(int));
            taskTable.Columns.Add("2. Location ID", typeof(int));
            taskTable.Columns.Add("Directions", typeof(string));

            foreach (Edge e in edges)
            {
                DataRow tableRow = taskTable.NewRow();
                tableRow["Id"] = e.ID;
                tableRow["Name"] = e.Name;
                tableRow["Weight"] = e.Weight;
                tableRow["Straight"] = e.Straight;
                tableRow["1. Location ID"] = e.L1;
                tableRow["2. Location ID"] = e.L2;
                tableRow["Directions"] = e.Direction;
                taskTable.Rows.Add(tableRow);
            }

            Session["TaskTable"] = taskTable;

            BindData();
        }

        private void gridVertices()
        {
            lblEditTitle.Text = "Edit Location";

            VertexWorker vw = new VertexWorker();
            List<Vertex> vertices = vw.GetAll();

            taskTable = new DataTable("TaskList");

            taskTable.Columns.Add("Id", typeof(int));
            taskTable.Columns.Add("Name", typeof(string));

            foreach (Vertex v in vertices)
            {
                DataRow tableRow = taskTable.NewRow();
                tableRow["Id"] = v.ID;
                tableRow["Name"] = v.Name;
                taskTable.Rows.Add(tableRow);
            }

            Session["TaskTable"] = taskTable;

            BindData();
        }

        private void gridRooms()
        {
            lblEditTitle.Text = "Edit Room";

            RoomWorker rw = new RoomWorker();
            List<Room> rooms = rw.GetAllRooms();

             taskTable = new DataTable("TaskList");

             taskTable.Columns.Add("Id", typeof(int));
             taskTable.Columns.Add("Name", typeof(string));
             taskTable.Columns.Add("Location ID", typeof(int));

             foreach(Room r in rooms)
             {
                 DataRow tableRow = taskTable.NewRow();
                 tableRow["Id"] = r.ID;
                 tableRow["Name"] = r.Name;
                 tableRow["Location ID"] = r.LocationID;
                 taskTable.Rows.Add(tableRow);
             }

             Session["TaskTable"] = taskTable;

             BindData();
        }

        protected void onChange(object sender, GridViewPageEventArgs e)
        {
            gridEdit.PageIndex = e.NewPageIndex;

            BindData();
        }

        protected void onEdit(object sender, GridViewEditEventArgs e)
        {
            gridEdit.EditIndex = e.NewEditIndex;

            BindData();
        }

        protected void onCancel(object sender, GridViewCancelEditEventArgs e)
        {
            gridEdit.EditIndex = -1;

            BindData();
        }

        protected void onUpdate(object sender, GridViewUpdateEventArgs e)
        {
            string item = Request.QueryString["action"];

            if (item == "Room")
                updateRoom(e);
            else if (item == "Location")
                updateVertices(e);
            else if (item == "Walkway")
                updateEdges(e);
        }

        private void updateEdges(GridViewUpdateEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];

            GridViewRow row = gridEdit.Rows[e.RowIndex];

            String id = ((TextBox)(row.Cells[1].Controls[0])).Text;
            String name = ((TextBox)(row.Cells[2].Controls[0])).Text;
            String weight = ((TextBox)(row.Cells[3].Controls[0])).Text;
            bool straight = ((CheckBox)(row.Cells[4].Controls[0])).Checked;
            String l1 = ((TextBox)(row.Cells[5].Controls[0])).Text;
            String l2 = ((TextBox)(row.Cells[6].Controls[0])).Text;
            String dir = ((TextBox)(row.Cells[7].Controls[0])).Text;

            dt.Rows[row.DataItemIndex]["Id"] = id;
            dt.Rows[row.DataItemIndex]["Name"] = name;
            dt.Rows[row.DataItemIndex]["Weight"] = weight;
            dt.Rows[row.DataItemIndex]["Straight"] = straight;
            dt.Rows[row.DataItemIndex]["1. Location ID"] = l1;
            dt.Rows[row.DataItemIndex]["2. Location ID"] = l2;
            dt.Rows[row.DataItemIndex]["Directions"] = dir;


            EdgeWorker vw = new EdgeWorker();

            int lId1;
            int lId2;
            int w;
            bool result1 = Int32.TryParse(l1, out lId1);
            bool result2 = Int32.TryParse(l2, out lId2);
            bool result3 = Int32.TryParse(weight, out w);
            if (!result1 || !result2 || !result3)
            {
                lblDone.Text = "The Location IDs and weight needs to be integers.";
                return;
            }

            vw.Edit(new Edge(Int32.Parse(id), name, dir, lId1, lId2, straight, w), User.Identity.Name.ToString());

            gridEdit.EditIndex = -1;

            BindData();
            lblDone.Text = "Success updating Walkway with ID: " + id + ". Current name " + name;
        }

        private void updateVertices(GridViewUpdateEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];

            GridViewRow row = gridEdit.Rows[e.RowIndex];

            String id = ((TextBox)(row.Cells[1].Controls[0])).Text;
            String name = ((TextBox)(row.Cells[2].Controls[0])).Text;

            dt.Rows[row.DataItemIndex]["Id"] = id;
            dt.Rows[row.DataItemIndex]["Name"] = name;

            VertexWorker vw = new VertexWorker();

            vw.Edit(new Vertex(Int32.Parse(id),name), User.Identity.Name.ToString());

            gridEdit.EditIndex = -1;

            BindData();
            lblDone.Text = "Success updating Location with ID: " + id + ". Current name " + name;
        }

        private void updateRoom(GridViewUpdateEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];

            GridViewRow row = gridEdit.Rows[e.RowIndex];
            
            String id = ((TextBox)(row.Cells[1].Controls[0])).Text;
            String name = ((TextBox)(row.Cells[2].Controls[0])).Text;
            String locationID = ((TextBox)(row.Cells[3].Controls[0])).Text;

            dt.Rows[row.DataItemIndex]["Id"] = id;
            dt.Rows[row.DataItemIndex]["Name"] = name;
            dt.Rows[row.DataItemIndex]["Location ID"] = locationID;

            RoomWorker rw = new RoomWorker();
            int lId;
            bool result = Int32.TryParse(locationID, out lId);
            if (!result)
            {
                lblDone.Text = "Invalid integer in Location ID.";
                return;
            }
            rw.Edit(new Room(Int32.Parse(id), name, lId), User.Identity.Name.ToString());

            gridEdit.EditIndex = -1;
            
            BindData();
            lblDone.Text = "Success updating Room with ID: " + id + ". Current name " + name;
        }

        protected void onDelete(Object sender, GridViewDeleteEventArgs e)
        {
            string item = Request.QueryString["action"];

            if (item == "Room")
                deleteRoom(e);
            else if (item == "Location")
                deleteVertex(e);
            else if (item == "Walkway")
                deleteEdge(e);
        }

        private void deleteEdge(GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];
            GridViewRow row = gridEdit.Rows[e.RowIndex];
            EdgeWorker ew = new EdgeWorker();

            int id = Int32.Parse(gridEdit.Rows[e.RowIndex].Cells[1].Text);
            String name = gridEdit.Rows[e.RowIndex].Cells[2].Text;

            ew.Delete(new Edge(id, name), User.Identity.Name.ToString());

            dt.Rows[row.DataItemIndex].Delete();

            BindData();
            lblDone.Text = "Success deleting Walkway with ID: " + id + " and name " + name;
        }

        private void deleteVertex(GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];
            GridViewRow row = gridEdit.Rows[e.RowIndex];
            VertexWorker vw = new VertexWorker();

            int id = Int32.Parse(gridEdit.Rows[e.RowIndex].Cells[1].Text);
            String name = gridEdit.Rows[e.RowIndex].Cells[2].Text;

            vw.Delete(new Vertex(id, name), User.Identity.Name.ToString());

            dt.Rows[row.DataItemIndex].Delete();

            BindData();
            lblDone.Text = "Success deleting Location with ID: " + id + " and name " + name;
        }

        private void deleteRoom(GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)Session["TaskTable"];
            GridViewRow row = gridEdit.Rows[e.RowIndex];
            RoomWorker rw = new RoomWorker();

            int id = Int32.Parse( gridEdit.Rows[e.RowIndex].Cells[1].Text );
            String name = gridEdit.Rows[e.RowIndex].Cells[2].Text;

            rw.Delete(new Room(id, name), User.Identity.Name.ToString());

            dt.Rows[row.DataItemIndex].Delete();

            BindData();
            lblDone.Text = "Success deleting Room with ID: " + id + " and name " + name;
        }

        private void BindData()
        {
            gridEdit.DataSource = Session["TaskTable"];
            gridEdit.DataBind();
        }
    }
}	